#include <fstream>
#include <iostream>
using namespace std;
#define max_N 100
#define max_M 100
#define MIN(a,b) (a>b?b:a)
int tile[max_N][max_M];
int N = 0;
int M = 0;
bool judge(int nx,int ny){
    if (nx < 0 || nx >= N || ny< 0|| ny>=M || tile[nx][ny]==1)
        return false;
    else
        return true;
}

int dx[4] = {-1, 0, 0, 1};
int dy[4] = {0, -1, 1, 0};
int max_step = 11;
int ans = max_step;
int dfs(int x,int y,int n)
{
    if (n>=max_step||ans<n)
        return max_step;
    int result = max_step;
    for (int i = 0; i < 4;++i){
        int nx = x + dx[i];
        int ny = y + dy[i];
        // 第一个就gg
        if (!judge(nx,ny))
            continue;
        while(judge(nx,ny)){
            if (tile[nx][ny]==3){
                result = MIN(result, n + 1);
                ans = MIN(ans, result);
                return result;
            }
            // if (tile[nx][ny]==2){
            //     break;
            // }
            nx += dx[i];
            ny += dy[i];
        }
        //
        if (nx < 0 || nx >= N || ny< 0|| ny>=M)
            continue;
        // 考虑在这开始新的
        if (tile[nx][ny] ==1){
            tile[nx][ny] = 0;
            result = MIN(result,dfs(nx - dx[i], ny - dy[i], n+1));
            tile[nx][ny] = 1;
        }
    }
    return result;
}

int main()
{
    //fstream cin("test.txt");
    while(cin>>M>>N){
        if (N==0||M==0)
            break;
        int nx = 0;
        int ny = 0;
        for (int i = 0; i < N;++i)
            for (int j = 0; j < M;++j){
                cin >> tile[i][j];
                if (tile[i][j] == 2){
                    nx = i;
                    ny = j;
                    tile[i][j] = 0; 
                }
            }
        ans = max_step;
        dfs(nx, ny, 0);
        if (ans >10)
            ans = -1;
        cout << ans<<endl;
        ans = max_step;
    }
    return 0;
}